#!/bin/bash


## layout schematic
libraryname1=$1
cellname1=$2
## source schematic
libraryname2=$3
cellname2=$4

##### run set path #####
run_path="/projects/analog_ip/workspace/xwqiu/cl/tacoma6/verify/SVS"
virtuoso_run_path="/projects/analog_ip/workspace/xwqiu/cl/tacoma6/cadence"

##### rule path #####
#lvs_rule="/projects/analog_ip/workspace/xwqiu/cl/tacoma6/verify/SVS/DFM_LVS_RC_CALIBRE_N6_1p13M_1X1Xa1Ya5Y2Yy2R_ALRDL.1.0b"
lvs_rule="/projects/analog_ip/workspace/xwqiu/cl/tacoma6/verify/SVS/virtuoso_option_T6.rule"


##### mkdir dir ######
cd ${run_path}

if [[ -d "${run_path}/CDL" ]]; then
	echo "${run_path}/CDL is exists."
else
	echo "${run_path}/CDL not exist."
        mkdir CDL
fi

if [[ -d "${run_path}/result" ]]; then
	echo "${run_path}/result is exists."
else
	echo "${run_path}/result not exist."
        mkdir result
fi

######### strm out CDL #########
cdslib="${virtuoso_run_path}/cds.lib"

#### out CDL1 ####
viewname="schematic"
cdl_file1="${cellname1}_1.netlist"
echo_f() {
	echo "simLibName = \"${libraryname1}\"" >> ${run_path}/si.env
	echo "simCellName = \"${cellname1}\"" >> ${run_path}/si.env
	echo "simViewName = \"${viewname}\"" >> ${run_path}/si.env
	echo "hnlNetlistFileName = \"${cdl_file1}\"" >> ${run_path}/si.env
	
	si ${run_path} -batch -command netlist -cdslib ${cdslib}

	cd ${run_path}
	if [[ -d "${run_path}/CDL" ]]; then
		mv ./${cdl_file1} ./CDL/${cdl_file1}
	else
		mkdir ./CDL
		mv ./${cdl_file1} ./CDL/${cdl_file1}
	fi
}
if [[ -f "${run_path}/si.conf" ]]; then
	echo "Exists"
	cp ${run_path}/si.conf ${run_path}/si.env
	echo_f
else
	echo "Not Exists"
	echo "Can't create si.env"
fi 


#### out CDL2 ####
viewname="schematic"
cdl_file2="${cellname2}_2.netlist"

echo_f() {
	echo "simLibName = \"${libraryname2}\"" >> ${run_path}/si.env
	echo "simCellName = \"${cellname2}\"" >> ${run_path}/si.env
	echo "simViewName = \"${viewname}\"" >> ${run_path}/si.env
	echo "hnlNetlistFileName = \"${cdl_file2}\"" >> ${run_path}/si.env
	
	si ${run_path} -batch -command netlist -cdslib ${cdslib}

	cd ${run_path}
	if [[ -d "${run_path}/CDL" ]]; then
		mv ./${cdl_file2} ./CDL/${cdl_file2}
	else
		mkdir ./CDL
		mv ./${cdl_file2} ./CDL/${cdl_file2}
	fi
}
if [[ -f "${run_path}/si.conf" ]]; then
	echo "Exists"
	cp ${run_path}/si.conf ${run_path}/si.env
	echo_f
else
	echo "Not Exists"
	echo "Can't create si.env"
fi


######### run LVS ##########
cd ${run_path}

cdl_file1_path="${run_path}/CDL/${cdl_file1}"
cdl_file2_path="${run_path}/CDL/${cdl_file2}"


echo "LAYOUT PRIMARY '${cellname1}'" > ./LVS.cal
echo "LAYOUT PATH '${cdl_file1_path}'" >> ./LVS.cal
echo "LAYOUT SYSTEM SPICE" >> ./LVS.cal

echo "SOURCE PRIMARY '${cellname2}'" >> ./LVS.cal
echo "SOURCE PATH '${cdl_file2_path}'" >> ./LVS.cal
echo "SOURCE SYSTEM SPICE" >> ./LVS.cal

echo "INCLUDE '${lvs_rule}'" >> ./LVS.cal


cd ./result
/apps/mentor/Calibre_2019.3_25.15/aoi_cal_2019.3_25.15/bin/calibre -lvs -hier -hyper -turbo 32 ../LVS.cal | tee DRC.log